#include <iostream>
#include <vector>
using namespace std;
#define int long long
vector<vector<int>> a(110, vector<int>(110, 99999999));
vector<int> w;
signed main() {
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++) {
        int x, y, z;
        cin >> x >> y >> z;
        w.push_back(x);
        if(y > 0)
        {
            a[i][y] = 1;
            a[y][i] = 1;
        }
        if(z > 0) {
            a[i][z] = 1;
            a[z][i] = 1;
        }
    }
    for(int k = 1; k <= n; k++) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                if(a[i][k] < 99999999 && a[j][k] < 99999999) {
                    a[i][j] = min(a[i][j],(a[i][k] + a[j][k]));
                }
            }
        }
    }
    int MIN = 0x3f3f3f3f;
    for(int i = 1; i <= n; i++) {
        int sum = 0;
        for(int j = 1; j <= n; j++) {
            if(i != j) {
                sum += w[j - 1] * a[i][j];
            }
        }
        MIN = min(MIN, sum);
    }
    cout << MIN << endl;    
    return 0;
}